package com.newer.util;



import com.newer.dao.Mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 通用CRUD操作类
 */
public class JdbcTemplate {
    private Connection conn = null; // 连接对象
    private PreparedStatement ps = null; // 语句对象
    private ResultSet rs = null; // 结果集对象

    // 通用的查询总记录数
    public Integer getCount(String sql,Object...params){
        Integer count = 0;

        try {
            conn = DruidUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if(params!=null){
                for(int i=0;i<params.length;i++){
                    ps.setObject(i+1,params[i]);
                }
            }

            rs = ps.executeQuery();

            while (rs.next()){
                count = rs.getInt(1);
            }

        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DruidUtil.closeAll(rs,ps,conn);
        }

        return  count;
    }

    // 通用的修改  ...动态参数
    public boolean update(String sql, Object... params) {
        boolean flag = false;

        try {
            conn = DruidUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            int rows = ps.executeUpdate();

            if (rows > 0) {
                flag = true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.closeAll(rs, ps, conn);
        }

        return flag;
    }

    // 通用的单个信息查询
    public <T> T queryForObject(String sql, Mapper<T> mapper, Object...params) {
        T obj = null;
        try {
            conn = DruidUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }
            rs = ps.executeQuery();

            while (rs.next()) {
                if (obj == null) {
                    // 组合+策略模式： 不做具体的代码实现，由子类实现映射
                    obj =mapper.mapperRow(rs);
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.closeAll(rs, ps, conn);
        }

        return obj;
    }


    // 通用的集合查询
    public <T> List<T> queryList(String sql, Mapper<T> mapper, Object... params) {
        List<T> list = new ArrayList<>();

        try {
            conn = DruidUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if (params != null) {
                for (int i = 0; i < params.length; i++) {
                    ps.setObject(i + 1, params[i]);
                }
            }

            rs = ps.executeQuery();

            while (rs.next()) {
                T obj = mapper.mapperRow(rs);
                list.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DruidUtil.closeAll(rs, ps, conn);
        }

        return list;
    }

}
